/*
 *
 *      Copyright (c) 2018-2099, lipengjun All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the fly2you.cn developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: lipengjun (939961241@qq.com)
 *
 */
package com.platform.modules.wx.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.annotation.SysLog;
import com.platform.common.utils.RestResponse;
import com.platform.modules.wx.entity.MsgTemplateEntity;
import com.platform.modules.wx.form.TemplateMsgBatchForm;
import com.platform.modules.wx.service.MsgTemplateService;
import com.platform.modules.wx.service.TemplateMsgService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * 消息模板
 *
 * @author 李鹏军
 * @date 2019-11-12 18:30:15
 */
@RestController
@RequestMapping("/manage/msgTemplate")
public class MsgTemplateManageController {
    @Autowired
    private MsgTemplateService msgTemplateService;
    @Autowired
    private TemplateMsgService templateMsgService;
    @Autowired
    private WxMpService wxMpService;

    /**
     * 消息模板列表
     *
     * @param appid  appid
     * @param params params
     * @return RestResponse
     */
    @GetMapping("/list")
    @RequiresPermissions("wx:msgtemplate:list")
    public RestResponse list(@CookieValue String appid, @RequestParam Map<String, Object> params) {
        params.put("appid", appid);
        Page<MsgTemplateEntity> page = msgTemplateService.queryPage(params);

        return RestResponse.success().put("page", page);
    }


    /**
     * 消息模板信息
     *
     * @param id 主键
     * @return RestResponse
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("wx:msgtemplate:info")
    public RestResponse info(@PathVariable("id") String id) {
        MsgTemplateEntity msgTemplateEntity = msgTemplateService.getById(id);

        return RestResponse.success().put("msgTemplate", msgTemplateEntity);
    }

    /**
     * 消息模板信息
     *
     * @param name 名称
     * @return RestResponse
     */
    @GetMapping("/getByName")
    @RequiresPermissions("wx:msgtemplate:info")
    public RestResponse getByName(String name) {
        MsgTemplateEntity msgTemplateEntity = msgTemplateService.selectByName(name);

        return RestResponse.success().put("msgTemplate", msgTemplateEntity);
    }

    /**
     * 新增消息模板
     *
     * @param msgTemplateEntity 消息模板
     * @return RestResponse
     */
    @SysLog("新增消息模板")
    @PostMapping("/save")
    @RequiresPermissions("wx:msgtemplate:save")
    public RestResponse save(@RequestBody MsgTemplateEntity msgTemplateEntity) {
        msgTemplateService.save(msgTemplateEntity);

        return RestResponse.success();
    }

    /**
     * 修改消息模板
     *
     * @param msgTemplateEntity 消息模板
     * @return RestResponse
     */
    @SysLog("修改消息模板")
    @PostMapping("/update")
    @RequiresPermissions("wx:msgtemplate:update")
    public RestResponse update(@RequestBody MsgTemplateEntity msgTemplateEntity) {
        msgTemplateEntity.setUpdateTime(new Date());
        msgTemplateService.updateById(msgTemplateEntity);

        return RestResponse.success();
    }

    /**
     * 删除消息模板
     *
     * @param ids ids
     * @return RestResponse
     */
    @SysLog("删除消息模板")
    @PostMapping("/delete")
    @RequiresPermissions("wx:msgtemplate:delete")
    public RestResponse delete(@RequestBody String[] ids) {
        msgTemplateService.removeByIds(Arrays.asList(ids));

        return RestResponse.success();
    }

    /**
     * 同步公众号模板
     *
     * @param appid appid
     * @return RestResponse
     * @throws WxErrorException
     */
    @SysLog("同步公众号模板")
    @PostMapping("/syncWxTemplate")
    @RequiresPermissions("wx:msgtemplate:save")
    public RestResponse syncWxTemplate(@CookieValue String appid) throws WxErrorException {
        this.wxMpService.switchoverTo(appid);
        msgTemplateService.syncWxTemplate(appid);
        return RestResponse.success();
    }

    /**
     * 批量向用户发送模板消息
     *
     * @param appid appid
     * @param form  批量发送模板消息表单
     * @return RestResponse
     */
    @SysLog("批量向用户发送模板消息")
    @PostMapping("/sendMsgBatch")
    @RequiresPermissions("wx:msgtemplate:save")
    public RestResponse sendMsgBatch(@CookieValue String appid, @RequestBody TemplateMsgBatchForm form) {
        this.wxMpService.switchoverTo(appid);
        templateMsgService.sendMsgBatch(form, appid);
        return RestResponse.success();
    }
}
